<template>
  <el-card>
    <el-row>
      <el-col :span="8">
        <el-input
          placeholder="请输入商品名称"
          clearable
          v-model="queryInfo.qurey"
          @clear="getGoodsLists()"
        ></el-input>
      </el-col>
      <el-button
        class="left-button"
        type="primary"
        :icon="Search"
        @click="getGoodsLists()"
        >搜索</el-button
      >
      <el-col :span="4">
        <el-button type="primary" class="left-button" @click="getAddshop"
          >添加商品</el-button
        >
      </el-col>
    </el-row>
    <el-table
      :data="goodsList"
      style="width: 100%"
      border
      stripe
      class="table-top"
    >
      <el-table-column type="index" label="序号" width="80px"></el-table-column>
      <el-table-column prop="goods_id" label="商品ID" width="140px">
      </el-table-column>
      <el-table-column prop="goods_name" label="商品名称" width="750px">
      </el-table-column>
      <el-table-column prop="goods_price" label="商品价格(元)" width="140px">
      </el-table-column>
      <el-table-column prop="goods_weight" label="商品重量" width="140px">
      </el-table-column>
      <el-table-column prop="add_time" label="创建时间" width="140px">
        <template v-slot="{ row }">
          {{ $filters.filtersTimes(row.add_time) }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button
            type="primary"
            size="small"
            :icon="Edit"
            @click="handleDialogValue(row)"
          ></el-button>
          <el-button
            type="danger"
            size="small"
            :icon="Delete"
            @click="delgoods(row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="queryInfo.pagenum"
      v-model:page-size="queryInfo.pagesize"
      :page-sizes="[2, 5, 10, 15]"
      :size="queryInfo.pagenum"
      :disabled="disabled"
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      class="page-margin"
    />
  </el-card>
</template>

<script setup>
import { Delete, Edit, Search } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { getGoodsList, delgood } from '@/api/goods'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router'
const router = useRouter()
const queryInfo = ref({
  qurey: '',
  pagenum: 1,
  pagesize: 10
})
const goodsList = ref([])
const total = ref(0)
const getGoodsLists = async () => {
  const res = await getGoodsList(queryInfo.value)
  console.log(res)
  goodsList.value = res.goods
  total.value = res.total
}
getGoodsLists()
const handleSizeChange = (newSize) => {
  queryInfo.value.pagesize = newSize
  getGoodsLists()
}
const handleCurrentChange = (newPage) => {
  queryInfo.value.pagenum = newPage
  getGoodsLists()
}
const handleDialogValue = (row) => {
  console.log(row)
}
const delgoods = (goodsid) => {
  ElMessageBox.confirm('此操作将永久删除该商品, 是否继续?', 'Warning', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      await delgood(goodsid.goods_id)
      ElMessage({
        type: 'success',
        message: '删除成功'
      })
      getGoodsLists()
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '您取消了这次删除'
      })
    })
}
const getAddshop = () => {
  router.push('/addshop')
}
</script>

<style lang="scss" scoped>
.left-button {
  margin-left: 25px;
}
.table-top {
  margin: 30px 0px;
}
.page-margin {
  margin-left: 20px;
}
</style>
